/**
 * @Company 
 * @Title: LoginController.java 
 * @Package com.jbinfo.qingcaowan.app.controller 
 * @author lizhenwei
 * @date 2016-07-11 下午1:06:21 
 * @version V1.0   
 */
package com.jbinfo.qingcaowan.app.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jbinfo.qingcaowan.common.BusinessException;
import com.jbinfo.qingcaowan.common.JSONResult;
import com.jbinfo.qingcaowan.common.ExceptionConstants.BusinessExceptionCode;
import com.jbinfo.qingcaowan.exam.dto.ExamCircleDTO;
import com.jbinfo.qingcaowan.exam.dto.ExamQuestionDTO;
import com.jbinfo.qingcaowan.exam.dto.ExamScoreDTO;
import com.jbinfo.qingcaowan.exam.dto.ExamUserDTO;
import com.jbinfo.qingcaowan.exam.service.CircleService;
import com.jbinfo.qingcaowan.exam.service.ExamScoreService;
import com.jbinfo.qingcaowan.exam.service.ExamUserService;
import com.jbinfo.qingcaowan.exam.service.QuestionService;
import com.jbinfo.qingcaowan.user.dto.UserDTO;
import com.jbinfo.qingcaowan.user.service.UserService;

/**
 * @ClassName: LoginController
 * @Description: 用户controller
 * @author lizhenwei
 */
/**
 * @author Administrator
 *
 */
@Controller
public class ExamController {

	private static final Logger LOG = LoggerFactory.getLogger(ExamController.class);

	@Resource
	private UserService userService;
	
	@Resource
	private CircleService circleService;
	
	@Resource
	private QuestionService questionService;
	
	@Resource
	private ExamUserService examUserService;
	
	@Resource
	private ExamScoreService examScoreService;
	
	// 考试列表
	@RequestMapping(value = "/{client}/findCircleList")
	public @ResponseBody
	Object findCircleList(@PathVariable("client") String client, 
			HttpServletResponse response,
			UserDTO userDTO,
			HttpSession session, ExamCircleDTO examCircleDTO) {
		LOG.info("客户端为 " + client + " examCircleDTO：" + examCircleDTO.toString());
		
		if (userDTO.getUserId() == null || userDTO.getUserId().isEmpty()) {
			throw new BusinessException(BusinessExceptionCode.param_error, "用户ID不能为空！");
		}
		// 根据userId 查询orgId
		JSONResult userJson = (JSONResult) userService.getUserInfoById(userDTO);
		UserDTO user = userJson.getResultBean();
		String orgId = user.getUserOrg();
		// 查询未考试的
		examCircleDTO.setOrgId(orgId);
		examCircleDTO.setQueryDate(new Date());
		examCircleDTO.setUserId(userDTO.getUserId());
		
		return circleService.findNoExamCircleList(examCircleDTO);
	}
	
	// 显示试卷及答案选项
	@RequestMapping(value = "/{client}/findQuestion")
	public @ResponseBody
	Object findQuestion(@PathVariable("client") String client, HttpSession session, 
			ExamQuestionDTO questionDTO) {
		LOG.info("客户端为 " + client + " questionDTO：" + questionDTO.toString());
		
		return questionService.findQuestionInfo(questionDTO);
	}
	
	// 记录用户答题内容\如果该题是最后一题，则返回考试得分
	@RequestMapping(value = "/{client}/saveExamUser")
	public @ResponseBody
	Object saveExamUser(@PathVariable("client") String client, 
			HttpServletResponse response,
			ExamUserDTO examUserDTO) {
		LOG.info("客户端为 " + client + " examUserDTO：" + examUserDTO.toString());
		
		return examUserService.saveExamUser(examUserDTO);
	}
	
	// 查询用户答案内容及答案分析
	@RequestMapping(value = "/{client}/findExamUserAnaly")
	public @ResponseBody
	Object findExamUserAnaly(@PathVariable("client") String client, 
			HttpServletResponse response,
			ExamUserDTO examUserDTO) {
		LOG.info("客户端为 " + client + " examUserDTO：" + examUserDTO.toString());
		
		return examUserService.findExamUserAndAnaly(examUserDTO);
	}
	
	// 查询userId、examCode 进行成绩查询
	@RequestMapping(value = "/{client}/findUserExamScore")
	public @ResponseBody
	Object findUserExamScore(@PathVariable("client") String client, 
			HttpServletResponse response,
			ExamScoreDTO examScoreDTO,
			Integer currentPage, Integer pageSize) {
		LOG.info("客户端为 " + client + " examScoreDTO：" + examScoreDTO.toString());
		
		return examScoreService.findUserExamCode(examScoreDTO, currentPage, pageSize);
	}
	
	// 根据examCode 查询该考试已参加人数
	@RequestMapping(value = "/{client}/findExamUserNum")
	public @ResponseBody
	Object findExamUserNum(@PathVariable("client") String client, 
			HttpServletResponse response,
			ExamCircleDTO examCircleDTO) {
		
		LOG.info("客户端为 " + client + " examCircleDTO：" + examCircleDTO.toString());
		
		return circleService.findTotalExamNum(examCircleDTO);
	}

}
